import { Controller } from "../../mvc/core/Controller";
import { IStallModule } from "../../../modules/interfaces/IStallModule";
import { IView } from "../../mvc/interfaces/IView";
import { StallModule } from "../../../modules/StallModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { GameProxy } from "../mvc/GameProxy";
import { SoundManager } from "../../common/sound/SoundManager";
import { SoundTypeConst } from "../../common/sound/SoundTypeConst";
import { AIManager } from "../scene/ai/AIManager";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ScreenOperation } from "../scene/ScreenOperation";
import { Cache } from "../cache/Cache";
import { SellItemData } from "../view/stall/defin/SellItemData";
import { ItemData } from "../resource/info/ItemData";
import { BuyItemData } from "../view/stall/defin/BuyItemData";
//class StallController
    
    export  class StallController extends Controller
    {
       

        public get isSelf(): Boolean
        {
            return this._isSelf;
        }

        public set isInStall(arg1: Boolean)
        {
            this._isInStall = arg1;
            (view as IStallModule).isInStall = arg1;
            return;
        }

        public get isInStall(): Boolean
        {
            return this._isInStall;
        }

        protected /* override */ initView(): IView
        {
            if (!this._stallModule) 
            {
                this._stallModule = new StallModule();
                this._stallModule.addEventListener(WindowEvent.CLOSE, this.hideStallHandler);
            }
            return this._stallModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.StallReady, this.readyHandler);
            Dispatcher.addEventListener(EventName.StallEndStall, this.endStallHandler);
            Dispatcher.addEventListener(EventName.StallOpenStall, this.openStallHandler);
            Dispatcher.addEventListener(EventName.StallCloseStall, this.closeStallHandler);
            NetDispatcher.addCmdListener(ServerCommand.StallOpenSuccess, this.openSuccessHandler);
            return;
        }

        /* internal  */resetListenerState(): void
        {
            this.hideStallHandler();
            this.addStallListeners();
            return;
        }

        /* internal  */addStallListeners(): void
        {
            this.updateMoney();
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            Dispatcher.addEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.addEventListener(EventName.StallRequestStart, this.startStallHandler);
            Dispatcher.addEventListener(EventName.StallFastOp, this.stallFashOpHandler);
            if (this.isSelf) 
            {
                Dispatcher.addEventListener(EventName.StallAddItem, this.addItemHandler);
                Dispatcher.addEventListener(EventName.StallDownItem, this.downItemHandler);
                Dispatcher.addEventListener(EventName.StallChangeName, this.changeNameHandler);
                Dispatcher.addEventListener(EventName.StallChangeBoothMsg, this.stallChangeBoothMsg);
                Dispatcher.addEventListener(EventName.StallClearMsg, this.clearMsgHandler);
                NetDispatcher.addCmdListener(ServerCommand.StallItemsUpdate, this.updateItems);
                NetDispatcher.addCmdListener(ServerCommand.StallMyStallNameUpdate, this.onUpdateMyStallName);
                NetDispatcher.addCmdListener(ServerCommand.StallMyIntroduceUpdate, this.onUpdateMyIntroduce);
                NetDispatcher.addCmdListener(ServerCommand.StallSelfLeaveWordsUpdate, this.onUpdateSelfLeaveWords);
                NetDispatcher.addCmdListener(ServerCommand.StallSelfLeaveWordsClear, this.onClearSelfLeaveWords);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.StallBuyItem, this.onBuyItem);
                Dispatcher.addEventListener(EventName.StallUserCloseStall, this.onUserCloseStall);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherItemsUpdate, this.updateOtherItems);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherLeaveWordsUpdate, this.onUpdateOtherLeaveWords);
                NetDispatcher.addCmdListener(ServerCommand.StallOtherLeaveWordsClear, this.onClearOtherLeaveWords);
            }
            return;
        }

        /* internal  */hideStallHandler(arg1: WindowEvent=null): void
        {
            Dispatcher.removeEventListener(EventName.StallRequestStart, this.startStallHandler);
            Dispatcher.removeEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.removeEventListener(EventName.StallAddItem, this.addItemHandler);
            Dispatcher.removeEventListener(EventName.StallDownItem, this.downItemHandler);
            Dispatcher.removeEventListener(EventName.StallChangeName, this.changeNameHandler);
            Dispatcher.removeEventListener(EventName.StallFastOp, this.stallFashOpHandler);
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateMoney);
            NetDispatcher.removeCmdListener(ServerCommand.StallItemsUpdate, this.updateItems);
            NetDispatcher.removeCmdListener(ServerCommand.StallMyStallNameUpdate, this.onUpdateMyStallName);
            Dispatcher.removeEventListener(EventName.StallBuyItem, this.onBuyItem);
            Dispatcher.removeEventListener(EventName.StallUserCloseStall, this.onUserCloseStall);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherItemsUpdate, this.updateOtherItems);
            NetDispatcher.removeCmdListener(ServerCommand.StallSelfLeaveWordsUpdate, this.onUpdateSelfLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallSelfLeaveWordsClear, this.onClearSelfLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherLeaveWordsUpdate, this.onUpdateOtherLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallOtherLeaveWordsClear, this.onClearOtherLeaveWords);
            NetDispatcher.removeCmdListener(ServerCommand.StallMyIntroduceUpdate, this.onUpdateMyIntroduce);
            Dispatcher.removeEventListener(EventName.StallAddMsg, this.addMsgHandler);
            Dispatcher.removeEventListener(EventName.StallClearMsg, this.clearMsgHandler);
            return;
        }

        /* internal  */readyHandler(arg1: DataEvent): void
        {
            if (!view.isHide && this.isSelf) 
            {
                return;
            }
            if (this._isInStall) 
            {
                this.isSelf = true;
                view.show();
                this.isInStall = true;
                this.resetListenerState();
                this.resetSelfStallName();
                this.updateList();
                this.updateSelfStallBoothMessage();
                return;
            }
            this.isSelf = true;
            view.show();
            this.isInStall = false;
            this.resetListenerState();
            this.resetSelfStallName();
            NetDispatcher.addCmdListener(ServerCommand.StallStartStallResult, this.onStartResult);
            this.updateList();
            return;
        }

        /* internal  */openStallHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            if (EntityUtil.toString(loc1) != EntityUtil.toString(cache.role.entityInfo.entityId)) 
            {
                GameProxy.stallProxy.openStall(loc1);
            }
            else if (this._isInStall) 
            {
                view.show();
                this.isSelf = true;
                this.isInStall = true;
                this.resetListenerState();
                this.resetSelfStallName();
                this.updateList();
                this.updateSelfStallBoothMessage();
                return;
            }
            return;
        }

        /* internal  */openSuccessHandler(arg1: DataEvent): void
        {
            this.isSelf = false;
            (view as IStallModule).showOther();
            this.resetListenerState();
            this.clearStallView();
            return;
        }

        /* internal  */closeStallHandler(arg1: DataEvent): void
        {
            GameProxy.stallProxy.closeStall();
            view.hide();
            return;
        }

        /* internal  */clearStallView(): void
        {
            return;
        }

        /* internal  */resetSelfStallName(): void
        {
            if (cache.pack.stallPackCache.stallName) 
            {
                this.udpateStallName(cache.pack.stallPackCache.stallName, true);
            }
            else 
            {
                GameProxy.stallProxy.myStallName();
            }
            return;
        }

        /* internal  */onUpdateMyStallName(arg1: string): void
        {
            this.udpateStallName(arg1, true);
            return;
        }

        /* internal  */onUpdateMyIntroduce(arg1: string): void
        {
            this.updateStallIntroduce(arg1, true);
            return;
        }

        /* internal  */onStartResult(arg1: Boolean): void
        {
            this.isInStall = arg1;
            if (arg1) 
            {
                SoundManager.instance.soundPlay(SoundTypeConst.SetUpAStall);
            }
            return;
        }

        /* internal  */startStallHandler(arg1: DataEvent): void
        {
            var e: DataEvent;

            var loc1;
            e = arg1;
            AIManager.cancelAll();
            if (RolePlayer.instance.isTurned) 
            {
                MsgManager.showRollTipsMsg(Language.getString(40800));
                return;
            }
            if (cache.battle.isInBattle) 
            {
                MsgManager.showRollTipsMsg(Language.getString(40801));
                return;
            }
            if (RolePlayer.instance.isCanStall() == false) 
            {
                MsgManager.showRollTipsMsg(Language.getString(40802));
                return;
            }
            if (RolePlayer.instance.isDoubleRest) 
            {
                ScreenOperation.interruptDoubleRest((): void
                {
                    startStall();
                    return;
                })
                return;
            }
            if (Cache.instance.fish.isInFishing) 
            {
                ScreenOperation.interruptFish((): void
                {
                    startStall();
                    return;
                })
                return;
            }
            this.startStall();
            return;
        }

        /* internal  */startStall(): void
        {
            this.isSelf = true;
            view.show();
            this.resetListenerState();
            GameProxy.stallProxy.startStall();
            return;
        }

        /* internal  */endStallHandler(arg1: DataEvent=null): void
        {
            view.hide();
            GameProxy.stallProxy.endStall();
            this.isInStall = false;
            return;
        }

        /* internal  */updateList(arg1: Boolean=false): void
        {
            (view as IStallModule).refreshItemList(cache.pack.stallPackCache.getStallItemsAtPage(1), true);
            if (arg1 && cache.pack.stallPackCache.validatelength == 0) 
            {
                this.endStallHandler();
            }
            return;
        }

        /* internal  */addItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SellItemData;
            if (Cache.instance.pack.stonePackCache.getIndexByUid(loc1.uid) > 0) 
            {
                loc1.fromIndex = cache.pack.stonePackCache.getIndexByUid(loc1.uid);
            }
            else 
            {
                loc1.fromIndex = cache.pack.backPackCache.getIndexByUid(loc1.uid);
            }
            GameProxy.stallProxy.upItem(loc1);
            return;
        }

        /* internal  */downItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1) 
            {
                GameProxy.stallProxy.downItem(loc1.uid);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(40803));
            }
            return;
        }

        /* internal  */stallFashOpHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (this.isSelf) 
            {
                if (loc1) 
                {
                    GameProxy.stallProxy.downItem(loc1.uid);
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(40803));
                }
            }
            else 
            {
                (view as IStallModule).showBuyItemWindow(loc1);
            }
            return;
        }

        /* internal  */updateSelfStallBoothMessage(): void
        {
            this.updateStallIntroduce(cache.pack.stallPackCache.stallIntroduce, true);
            (view as IStallModule).clearLeaveMsg(true);
            this.updateLeaveWords(cache.pack.stallPackCache.leaveWords, true);
            return;
        }

        /* internal  */changeNameHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.stallProxy.modifyStallName(loc1);
            return;
        }

        /* internal  */addMsgHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            if (this.isSelf) 
            {
                GameProxy.stallProxy.addLeaveMsg(loc1, cache.role.entityInfo.entityId);
            }
            else 
            {
                GameProxy.stallProxy.addLeaveMsg(loc1, cache.stallOtherCache.entityId);
            }
            return;
        }

        /* internal  */stallChangeBoothMsg(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.stallProxy.modifyStallIntroduce(loc1);
            return;
        }

        /* internal  */clearMsgHandler(arg1: DataEvent): void
        {
            GameProxy.stallProxy.clearLeaveMsg();
            return;
        }

        /* internal  */openStall(arg1: SEntityId, arg2: string): void
        {
            this.udpateStallName(arg2, false);
            this.isSelf = false;
            GameProxy.stallProxy.openStall(arg1);
            return;
        }

        /* internal  */udpateStallName(arg1: string, arg2: Boolean): void
        {
            (view as IStallModule).setStallName(arg1, arg2);
            return;
        }

        /* internal  */updateStallIntroduce(arg1: string, arg2: Boolean): void
        {
            (view as IStallModule).setStallIntroduce(arg1, arg2);
            return;
        }

        /* internal  */updateMoney(arg1: Object=null): void
        {
            var loc1=cache.login.loginGame.money;
            (view as IStallModule).updateMoney(loc1.coin, loc1.gold);
            return;
        }

        /* internal  */updateItems(arg1: Object): void
        {
            if (arg1) 
            {
                this.updateList(true);
            }
            else 
            {
                this.updateList(false);
            }
            return;
        }

        /* internal  */onUpdateSelfLeaveWords(arg1: Array<any>): void
        {
            this.updateLeaveWords(arg1, true);
            return;
        }

        /* internal  */onUpdateOtherLeaveWords(arg1: Array<any>): void
        {
            this.updateLeaveWords(arg1, false);
            return;
        }

        /* internal  */updateLeaveWords(arg1: Array<any>, arg2: Boolean=true): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=arg1;
            for(loc1 of loc3) 
            {
                if (loc1.type == 0) 
                {
                    (view as IStallModule).addBuyMsg(loc1.leaveWordDt, loc1.player, loc1.gold, loc1.coin, loc1.amount, loc1.playerItem, arg2);
                    continue;
                }
                if (loc1.type != 1) 
                {
                    continue;
                }
                (view as IStallModule).addLeaveWord(loc1.leaveWordDt, loc1.player, loc1.msg, arg2);
            }
            return;
        }

        /* internal  */onBuyItem(arg1: DataEvent): void
        {
            var loc1=arg1.data as BuyItemData;
            GameProxy.stallProxy.buyItem(loc1.uid, loc1.buyNum, loc1.coin, loc1.gold);
            return;
        }

        /* internal  */onClearOtherLeaveWords(arg1: Object): void
        {
            (view as IStallModule).clearLeaveMsg(false);
            return;
        }

        /* internal  */onClearSelfLeaveWords(arg1: Object): void
        {
            (view as IStallModule).clearLeaveMsg(true);
            return;
        }

        /* internal  */updateOtherItems(arg1: Object): void
        {
            (view as IStallModule).setStallName(cache.stallOtherCache.stallName, false);
            (view as IStallModule).setStallIntroduce(cache.stallOtherCache.stallIntroduce, false);
            (view as IStallModule).refreshItemList(cache.stallOtherCache.getStallItemsAtPage(1), false);
            (view as IStallModule).updatePlayerMiniInfo(cache.stallOtherCache.miniInfo);
            return;
        }

        /* internal  */onUserCloseStall(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            if (!view.isHide && !this._isSelf && EntityUtil.toString(loc1) == EntityUtil.toString(cache.stallOtherCache.entityId)) 
            {
                view.hide();
                MsgManager.showRollTipsMsg(Language.getString(40804));
            }
            return;
        }

        public set isSelf(arg1: Boolean)
        {
            this._isSelf = arg1;
            return;
        }

        private /* var */_stallModule: IStallModule;

        private /* var */_isSelf: Boolean=false;

        private /* var */_isInStall: Boolean=false;
    }
